package org.ctsi.rsds.base.filter;

import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.RpcException;

public class ComsumerFilter implements Filter {
	
	private static final String USER_TOKEN = "user_token";
	private static final String DEFAULT_TOKEN = "NAJPEJBOJAFNNAJMLKNJFLKSJF";

	@Override
	public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
		
		// before do somthing...
		RpcContext context = RpcContext.getContext();

		System.out.println("ComsumerFilter:" + context.hashCode());

		String token = getToken(context);

		if (token != null) {
			context.setAttachment(USER_TOKEN, token);
		}

		Result result = invoker.invoke(invocation);

		return result;
	}
	
	private String getToken(RpcContext context){
		String token = null;
		if(context.getLocalAddressString() != null){
			token = DEFAULT_TOKEN;
		}
		return token;
	}

}
